<template>
  <el-container>
    <!-- <el-header height="10px">
      <img src="@/assets/images/login_logo.png" alt=""/>
    </el-header> -->
    <el-main :style="{backgroundImage: `url(${imgUrl})`}" class="loginMain">
      <div style="position: absolute;top: 10%;left: 50%;width: 800px;transform: translateX(-50%);">
        <!-- <img style="height: 50px;width: 700px;" src="@/assets/images/login_logo.png" alt=""/> -->
         <div class="art-text">风电机组关键构件疲劳寿命预测与评价系统</div>
      </div>
      <div style="position: relative;left: 10%;top: 0%;"><img style="width: 475x;height: 85px;" src="@/assets/images/dflogo.png" alt=""/></div>
      <!-- <div class="login" :style="'background-image: url(' + require('@/assets/images/newbg.jpg') + ')'"> -->
      <div class="login">
        <el-form ref="loginForm" :model="loginForm" :rules="loginRules" class="login-form" label-position="top">
          <div class="title">
            <img src="@/assets/images/login_ico0.png" alt=""/>
            用户登录
            <span>UserLogin</span>
          </div>
          <div class="form-title">
            <img src="@/assets/images/login_ico1.png" alt=""/>
            用户名：
          </div>
          <el-form-item prop="username">
            <el-input
              v-model="loginForm.username"
              type="text"
              auto-complete="off"
            />
          </el-form-item>
          <div class="form-title">
            <img src="@/assets/images/login_ico2.png" alt=""/>
            密<span style="padding-left: 19px"></span>码：
          </div>
          <el-form-item prop="password">
            <el-input
              v-model="loginForm.password"
              type="password"
              auto-complete="off"
              @keyup.enter.native="handleLogin"
              show-password
            />
          </el-form-item>
          <div class="form-title" v-if="captchaEnabled">
            <img src="@/assets/images/login_ico3.png" alt=""/>
            验证码：
          </div>
          <el-form-item prop="code" v-if="captchaEnabled">
            <el-input
              v-model="loginForm.code"
              auto-complete="off"
              style="width: 184px"
              @keyup.enter.native="handleLogin"
            />
            <div class="login-code">
              <img :src="codeUrl" @click="getCode" class="login-code-img"/>
            </div>
          </el-form-item>
          <el-form-item style="width:100%;">
            <el-button
              :loading="loading"
              size="medium"
              type="primary"
              style="width:100%;height: 47px;font-size:15px;"
              @click.native.prevent="handleLogin"
            >
              <span v-if="!loading">登 录</span>
              <span v-else>登 录 中...</span>
            </el-button>
            <div style="float: right;" v-if="register">
              <router-link class="link-type" :to="'/register'">立即注册</router-link>
            </div>
          </el-form-item>
        </el-form>
      </div>
    </el-main>
    <el-footer height="40px">
      <div class="el-login-footer">
        <div>技术支持|中国科学院金属研究所</div>
        <!-- <div>中国科学院金属研究所 | 材料与构件材料疲劳性能预测系统</div> -->
        <!-- <div>技术支持：024-63748889 建议使用 IE9.0以上版本</div> -->
      </div>
    </el-footer>
  </el-container>
</template>

<script>
  import {getCodeImg} from "@/api/login";
  import Cookies from "js-cookie";
  import {encrypt, decrypt} from '@/utils/jsencrypt'
  import {listConfig} from "@/api/system/config";
  import store from '@/store'
  import cache from '@/plugins/cache'
  import { getRouters } from '@/api/menu'

  const storageSetting = JSON.parse(localStorage.getItem('layout-setting')) || ''
  export default {
    name: "Login",
    data() {
      return {
        codeUrl: "",
        loginForm: {
          username: "",
          password: "",
          rememberMe: false,
          code: "",
          uuid: ""
        },
        loginRules: {
          username: [
            {required: true, trigger: "blur", message: "请输入您的账号"}
          ],
          password: [
            {required: true, trigger: "blur", message: "请输入您的密码"}
          ],
          code: [{required: true, trigger: "change", message: "请输入验证码"}]
        },
        loading: false,
        // 验证码开关
        captchaEnabled: false,
        // 注册开关
        register: false,
        redirect: undefined,
        dateRange: [],
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          configName: undefined,
          configKey: undefined,
          configType: undefined
        },
        imgUrl: require('@/assets/images/dfbg.jpg')
      };
    },
    watch: {
      $route: {
        handler: function (route) {
          this.redirect = route.query && route.query.redirect;
        },
        immediate: true
      }
    },
    created() {
      this.getCode();
      this.getCookie();
    },
    methods: {
      getCode() {
        // getCodeImg().then(res => {
        //   this.captchaEnabled = res.captchaEnabled === undefined ? true : res.captchaEnabled;
        //   if (this.captchaEnabled) {
        //     this.codeUrl = "data:image/gif;base64," + res.img;
        //     this.loginForm.uuid = res.uuid;
        //   }
        // });
        //加载皮肤和主题
        listConfig(this.addDateRange(this.queryParams, this.dateRange)).then(response => {
            var that = this
            response.rows.forEach(function (element) {
              if (element.configKey == "sys.index.sideTheme") {
                storageSetting.sideTheme = element.configValue
                store.dispatch('settings/changeSetting', {
                  key: 'sideTheme',
                  value: element.configValue
                })
              } else if (element.configKey == "sys.index.skinName") {
                let color = "#409EFF"
                switch (element.configValue) {
                  case "skin-red":
                    color = "#FF4500";
                    break;
                  case "skin-green":
                    color = "#409640";
                    break;
                  case "skin-yellow":
                    color = "#D5B925";
                    break;
                  case "skin-purple":
                    color = "#C47BA9";
                    break;
                  default:
                    color = "#409EFF"
                }
                storageSetting.theme = color
                store.dispatch('settings/changeSetting', {
                  key: 'theme',
                  value: color
                })

              } else if (element.configKey == "sys.index.background") {
                storageSetting.background = element.configValue === undefined ? 'bg1.jpg' : element.configValue
                // that.imgUrl = require('@/assets/images/' + storageSetting.background + '')
                store.dispatch('settings/changeSetting', {
                  key: 'background',
                  value: element.configValue
                })
              }
              cache.local.set(
                "layout-setting",
                `{
                  "topNav":true,
                  "tagsView":true,
                  "fixedHeader":${storageSetting.fixedHeader},
                  "sidebarLogo":${storageSetting.sidebarLogo},
                  "dynamicTitle":${storageSetting.dynamicTitle},
                  "sideTheme":"${storageSetting.sideTheme}",
                  "theme":"${storageSetting.theme}",
                  "background":"${storageSetting.background}"
                }`
              )
            });
          }
        );
      },
      getCookie() {
        const username = Cookies.get("username");
        const password = Cookies.get("password");
        const rememberMe = Cookies.get('rememberMe')
        this.loginForm = {
          username: username === undefined ? this.loginForm.username : username,
          password: password === undefined ? this.loginForm.password : decrypt(password),
          rememberMe: rememberMe === undefined ? false : Boolean(rememberMe)
        };
      },
      handleLogin() {
        this.$refs.loginForm.validate(valid => {
          if (valid) {
            this.loading = true;
            if (this.loginForm.rememberMe) {
              Cookies.set("username", this.loginForm.username, {expires: 30});
              Cookies.set("password", encrypt(this.loginForm.password), {expires: 30});
              Cookies.set('rememberMe', this.loginForm.rememberMe, {expires: 30});
            } else {
              Cookies.remove("username");
              Cookies.remove("password");
              Cookies.remove('rememberMe');
            }
            this.$store.dispatch("Login", this.loginForm).then(() => {
              // 向后端请求路由数据
              getRouters().then(res => {
                const sdata = JSON.parse(JSON.stringify(res.data))
                if(sdata.length>0){
                  this.$router.push({path: sdata[0].path+"/"+sdata[0].children[0].path}).catch(() => {
                  });
                }else{

                  this.$router.push({path: this.redirect || "/"}).catch(() => {
                  });
                }
              })

            }).catch(() => {
              this.loading = false;
              if (this.captchaEnabled) {
                this.getCode();
              }
            });
          }
        });
      }
    },
  };
</script>

<style rel="stylesheet/scss" lang="scss">

  body > .el-container {
    margin-bottom: 40px;
  }

  .el-header {
    img {
      position: relative;
      top: 22px;
      left: 10%
    }
  }

  .el-footer {
    text-align: center;
  }

  .el-header, .el-footer {
    color: #333;
    background-color: #2d88fc;
  }

  .loginMain.el-main {
    color: #333;
    height: calc(100vh - 40px);
    position: relative;
    background-position: center;
    display: flex !important;
    // justify-content: center;
    align-items: center;
    padding: 0;
  }

  .login {
    position: relative;
    left: 25%;
    width: 420px;
    height: 360px;
    border-radius: 2px;
    background: #ffffff;
    padding: 36px 36px 45px 47px;
  }

  .title {
    font-size: 22px;
    color: #2474BC;
    padding-left: 50px;
    position: relative;
    margin-bottom: 14px;
    font-family: MicrosoftYaHei;

    img {
      position: absolute;
      left: 0;
      top: -6px;
      width: 38px;
    }

    span {
      color: #C3C5C6;
      font-size: 16px;
      font-family: ArialMT;
    }
  }

  .login-form {
    .el-input {
      height: 42px;

      input {
        height: 42px;
      }
    }

    .input-icon {
      height: 39px;
      width: 14px;
      margin-left: 2px;
    }
  }

  .login-tip {
    font-size: 13px;
    text-align: center;
    color: #bfbfbf;
  }

  .login-code {
    width: 148px;
    height: 42px;
    float: right;

    img {
      width: 148px;
      height: 42px;
      cursor: pointer;
      vertical-align: middle;
    }
  }

  .el-login-footer {
    padding-top: 11px;
    height: 40px;
    text-align: center;
    color: #fff;
    font-family: Arial;
    font-size: 14px;
    letter-spacing: 1px;
  }

  .login-code-img {
    height: 38px;
  }

  .form-title {
    color: #506575;
    font-size: 14px;
    font-family: MicrosoftYaHei;
    position: relative;
    text-indent: 30px;
    margin-bottom: 4px;

    img {
      position: absolute;
      top: -2px;
      left: 0;
      width: 22px;
      height: 20px;
    }
  }
   .art-text {
      font-size: 40px;
      font-family: 'Arial Black', Gadget, sans-serif;
      color: #6ef4d3;
      font-weight: bold;
      text-transform: uppercase;
      line-height: 1.2;
      letter-spacing: 2px;
      overflow: hidden;
      text-shadow: 2px 2px 0 #3899f5;
    }
</style>
